<?php

/**
 * Description of Question
 *
 * @author hoangle
 */
class UserQuestion {

    private $db;
    
    public function  __construct() {
        $this->db = Zend_Registry::get('db');
    }

    /**
     * @desc Get list user question by userid
     * @param <type> $userid
     * @return <array> data user question
     */
    public function getUserQuestionById($id) {
        try {
            $sql = "SELECT * FROM tbluserquestions WHERE id=:id AND status=1";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':id', $id, PDO::PARAM_INT);
            $stmt->execute();
            $result = $stmt->fetch();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return $result;
    }

    /**
     * @desc Insert account info
     * @param <array> $arrParams
     * @return true | false
     */
    public function updateUserQuestion($arrParam) {
        try {
            $sql = "UPDATE tbluserquestions SET refuse=1, refuse_date=now(), count_refuse=count_refuse+1 WHERE
                id=:id AND userid=:userid AND date(now()) <> date(createtime) AND status=1
                 AND answer_flag=0";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':userid', $arrParam['userid'], PDO::PARAM_INT);
            $stmt->bindParam(':id', $arrParam['id'], PDO::PARAM_INT);
            $stmt->execute();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return true;
    }


    /**
     * @desc Get list user question by userid
     * @param <type> $userid
     * @return <array> data user question
     */
    public function getUserQuestionByUserId($arrParam) {
        try {
            $sql = "SELECT * FROM tbluserquestions WHERE status=1 AND userid=:userid
                AND answer_flag=1 AND refuse=0 AND (date_format(createtime, '%Y-%m-%d')
                <= date_format(now(),'%Y-%m-%d') AND date_format(createtime, '%Y-%m-%d')
                >= date_format(:yesterday, '%Y-%m-%d')) ORDER BY createtime DESC";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':userid', $arrParam['userid'], PDO::PARAM_INT);
            $stmt->bindParam(':yesterday', $arrParam['yesterday'], PDO::PARAM_STR);
            $stmt->execute();
            $result = $stmt->fetchAll();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return $result;
    }


    /**
     * @desc Insert account info
     * @param <array> $arrParams
     * @return true | false
     */
    public function refuseUserQuestionAlreadyReply($arrParam) {
        try {
            $sql = "UPDATE tbluserquestions SET refuse=1, refuse_flag=1, refuse_date=now(), count_refuse=count_refuse+1
                WHERE id=:id AND userid=:userid AND status=1 AND answer_flag=1 ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':id', $arrParam['id'], PDO::PARAM_INT);
            $stmt->bindParam(':userid', $arrParam['userid'], PDO::PARAM_INT);
            $result = $stmt->execute();
            $stmt->closeCursor();
            unset($stmt);

            //Check for refund balance
            if($result) {
                $this->creaseCreditByUserId($arrParam['userid']);
            }
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e->getMessage(), 11);
        }
        return true;
    }

    /**
     * @desc Decrease balance of user
     * @param <type> $arrData
     * @return <type>
     */
    public function creaseCreditByUserId($userid) {
        try {
            $sql = "UPDATE tblbalances SET totalcredit=totalcredit + 1 WHERE userid=:p_userid";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':p_userid', $userid, PDO::PARAM_INT);
            $stmt->execute();
            $stmt->closeCursor();
            unset($stmt);
        }
        catch (Zend_Db_Exception $e) {
            throw new Zend_Exception($e);
        }
        return true;
    }

}

?>
